# 27_utas2003_2017_participation_in_election.R
# Purpose: Ideological Extremism and Political Participation in Japan
# Created: 2021-3-14 Taka-aki Asano
# Last Modified: 2021-10-10

# package
require("dplyr")
require("ggplot2")
require("texreg")
quartzFonts(HiraKaku = quartzFont(rep('Hiragino Kaku Gothic Pro W3', 4)))
theme_set(theme_classic(base_size = 12, base_family = 'HiraKaku'))


# 2003
## merge
UTAS2003_Ideology <- voter2003[,c("id", "q020400", "f010100", "f020600", "f010200", "q010100", "Ideology")]
UTAS2003_Ideology <- as.data.frame(UTAS2003_Ideology)

## recode
UTAS2003_Ideology$Vote <- NA
UTAS2003_Ideology$Vote[UTAS2003_Ideology$q020400 == 4] <- 1
UTAS2003_Ideology$Vote[UTAS2003_Ideology$q020400 %in% c(1, 2, 3)] <- 0
UTAS2003_Ideology$Ideology_strong <- abs(
  UTAS2003_Ideology$Ideology - median(UTAS2003_Ideology$Ideology, na.rm = TRUE)
)
UTAS2003_Ideology$Support <- ifelse(UTAS2003_Ideology$q010100 == 10, 0, 1)
UTAS2003_Ideology$Female <- ifelse(UTAS2003_Ideology$f010100 == 0, 1, 0)
UTAS2003_Ideology$College <- ifelse(UTAS2003_Ideology$f020600 == 4, 1, 0)
UTAS2003_Ideology$Age <- NA
UTAS2003_Ideology$Age[UTAS2003_Ideology$f010200 %in% c(1, 2)] <- 1
UTAS2003_Ideology$Age[UTAS2003_Ideology$f010200 %in% c(3, 4)] <- 2
UTAS2003_Ideology$Age[UTAS2003_Ideology$f010200 == 5] <- 3
UTAS2003_Ideology$Age[UTAS2003_Ideology$f010200 == 6] <- 4
UTAS2003_Ideology$Age[UTAS2003_Ideology$f010200 == 7] <- 5
UTAS2003_Ideology$Age[UTAS2003_Ideology$f010200 == 8] <- 5
  
## regression
glm2003 <- glm(Vote ~ Ideology + Ideology_strong + Support + Female + College + Age, 
               data = UTAS2003_Ideology, family = binomial(link = "logit"))
summary(glm2003)


# 2009
## merge
UTAS2009_Ideology <- voter2009[,c("ID", "Q010100", "Q014600", "Q014800", "Q014700", "Q012700", "Ideology")]
UTAS2009_Ideology <- as.data.frame(UTAS2009_Ideology)

## recode
UTAS2009_Ideology$Vote <- NA
UTAS2009_Ideology$Vote[UTAS2009_Ideology$Q010100 == 4] <- 1
UTAS2009_Ideology$Vote[UTAS2009_Ideology$Q010100 %in% c(1, 2, 3)] <- 0
UTAS2009_Ideology$Ideology_strong <- abs(
  UTAS2009_Ideology$Ideology - median(UTAS2009_Ideology$Ideology, na.rm = TRUE)
)
UTAS2009_Ideology$Support <- ifelse(UTAS2009_Ideology$Q012700 == 9, 0, 1)
UTAS2009_Ideology$Female <- ifelse(UTAS2009_Ideology$Q014600 == 2, 1, 0)
UTAS2009_Ideology$College <- ifelse(UTAS2009_Ideology$Q014800 %in% c(5, 6), 1, 0)
UTAS2009_Ideology$Age <- UTAS2009_Ideology$Q014700

## regression
glm2009 <- glm(Vote ~ Ideology + Ideology_strong + Support + Female + College + Age, 
               data = UTAS2009_Ideology, family = binomial(link = "logit"))
summary(glm2009)


# 2012
## merge
UTAS2012_Ideology <- voter2012[,c("ID", "Q010100", "Q014100", "Q014300", "Q014200", "Q013700", "Ideology")]
UTAS2012_Ideology <- as.data.frame(UTAS2012_Ideology)

## recode
UTAS2012_Ideology$Vote <- NA
UTAS2012_Ideology$Vote[UTAS2012_Ideology$Q010100 == 2] <- 1
UTAS2012_Ideology$Vote[UTAS2012_Ideology$Q010100 == 1] <- 0
UTAS2012_Ideology$Ideology_strong <- abs(
  UTAS2012_Ideology$Ideology - median(UTAS2012_Ideology$Ideology, na.rm = TRUE)
)
UTAS2012_Ideology$Q013700[UTAS2012_Ideology$Q013700 == 99] <- NA
UTAS2012_Ideology$Support <- ifelse(UTAS2012_Ideology$Q013700 == 14, 0, 1)
UTAS2012_Ideology$Female <- ifelse(UTAS2012_Ideology$Q014100 == 2, 1, 0)
UTAS2012_Ideology$Q014300[UTAS2012_Ideology$Q014300 == 99] <- NA
UTAS2012_Ideology$College <- ifelse(UTAS2012_Ideology$Q014300 %in% c(5, 6), 1, 0)
UTAS2012_Ideology$Age <- UTAS2012_Ideology$Q014200

## regression
glm2012 <- glm(Vote ~ Ideology + Ideology_strong + Support + Female + College + Age, 
               data = UTAS2012_Ideology, family = binomial(link = "logit"))
summary(glm2012)


# 2014
## merge
UTAS2014_Ideology <- voter2014[,c("ID", "W1Q1", "W1F1", "W1F3", "W1F2", "W1Q18_1", "Ideology")]
UTAS2014_Ideology <- as.data.frame(UTAS2014_Ideology)

## recode
UTAS2014_Ideology$Vote <- NA
UTAS2014_Ideology$Vote[UTAS2014_Ideology$W1Q1 == 2] <- 1
UTAS2014_Ideology$Vote[UTAS2014_Ideology$W1Q1 == 1] <- 0
UTAS2014_Ideology$Ideology_strong <- abs(
  UTAS2014_Ideology$Ideology - median(UTAS2014_Ideology$Ideology, na.rm = TRUE)
)
UTAS2014_Ideology$W1Q18_1[UTAS2014_Ideology$W1Q18_1 == 99] <- NA
UTAS2014_Ideology$Support <- ifelse(UTAS2014_Ideology$W1Q18_1 == 11, 0, 1)
UTAS2014_Ideology$Female <- ifelse(UTAS2014_Ideology$W1F1 == 2, 1, 0)
UTAS2014_Ideology$W1F3[UTAS2014_Ideology$W1F3 == 99] <- NA
UTAS2014_Ideology$College <- ifelse(UTAS2014_Ideology$W1F3 %in% c(5, 6), 1, 0)
UTAS2014_Ideology$Age <- UTAS2014_Ideology$W1F2
UTAS2014_Ideology$Age[UTAS2014_Ideology$W1F2 == 99] <- NA

## regression
glm2014 <- glm(Vote ~ Ideology + Ideology_strong + Support + Female + College + Age, 
               data = UTAS2014_Ideology, family = binomial(link = "logit"))
summary(glm2014)


# 2017
## merge
UTAS2017_Ideology <- voter2017[,c("ID", "Q1", "F1", "F3", "F2", "Q18", "Ideology")]
UTAS2017_Ideology <- as.data.frame(UTAS2017_Ideology)

## recode
UTAS2017_Ideology$Vote <- NA
UTAS2017_Ideology$Vote[UTAS2017_Ideology$Q1 == 2] <- 1
UTAS2017_Ideology$Vote[UTAS2017_Ideology$Q1 == 1] <- 0
UTAS2017_Ideology$Ideology_strong <- abs(
  UTAS2017_Ideology$Ideology - median(UTAS2017_Ideology$Ideology, na.rm = TRUE)
)
UTAS2017_Ideology$Q18[UTAS2017_Ideology$Q18 == 99] <- NA
UTAS2017_Ideology$Support <- ifelse(UTAS2017_Ideology$Q18 == 10, 0, 1)
UTAS2017_Ideology$Female <- ifelse(UTAS2017_Ideology$F1 == 2, 1, 0)
UTAS2017_Ideology$F3[UTAS2017_Ideology$F3 == 99] <- NA
UTAS2017_Ideology$College <- ifelse(UTAS2017_Ideology$F3 %in% c(5, 6), 1, 0)
UTAS2017_Ideology$Age <- UTAS2017_Ideology$F2
UTAS2017_Ideology$Age[UTAS2017_Ideology$F2 == 99] <- NA
UTAS2017_Ideology$Age <- recode(UTAS2017_Ideology$Age, 
                                `1` = 1, `2` = 1, `3` = 2, `4` = 3, 
                                `5` = 4, `6` = 5, `7` = 6)

## regression
glm2017 <- glm(Vote ~ Ideology + Ideology_strong + Support + Female + College + Age, 
               data = UTAS2017_Ideology, family = binomial(link = "logit"))
summary(glm2017)


# plot
glmAll <- data.frame(
  Year = rep(c(2003, 2009, 2012, 2014, 2017), each = 7), 
  Variable = rep(c("Intercept", "Ideology", "Ideological Extremism", 
                   "Partisanship", "Female", "College Education", "Age"), times = 5), 
  Estimate = c(coef(glm2003), coef(glm2009), coef(glm2012), coef(glm2014), coef(glm2017)), 
  SE = c(summary(glm2003)$coefficient[,2], summary(glm2009)$coefficient[,2], 
         summary(glm2012)$coefficient[,2], summary(glm2014)$coefficient[,2], 
         summary(glm2017)$coefficient[,2])
)
glmAll$Variable <- factor(glmAll$Variable, 
                          levels = c("Intercept", "Ideology", "Ideological Extremism", 
                                     "Partisanship", "Female", "College Education", "Age"))
glmAll_plot <- ggplot(glmAll[glmAll$Variable %in% c("Ideology", "Ideological Extremism"),], 
                      aes(x = as.factor(Year), y = Estimate, 
                          ymin = Estimate - 1.96*SE, 
                          ymax = Estimate + 1.96*SE)) + 
  geom_hline(yintercept = 0, linetype = "dashed",colour = "gray") + 
  geom_pointrange() + facet_wrap(~Variable, nrow = 9, scales = "free") + 
  labs(x = "Year", y = "Logistic Regression Coefficients", 
       title = "Election Turnout (UTAS)") + 
  theme(plot.title = element_text(hjust = 0.5))
plot(glmAll_plot)
